package com.zoffy.wmsbackend.security;

import com.zoffy.wmsbackend.model.entity.User;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

@Data
public class UserDetail implements UserDetails {
    private User user;

    public UserDetail(User user) {
        this.user = user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of(new GrantedAuthority() {
            @Override
            public String getAuthority() {
                return user.getRole();
            }
        });
    }

    @Override
    public String getPassword() {
        if(user==null){
            return null;
        }
        return user.getUserPassword();
    }

    @Override
    public String getUsername() {
        if(user==null){
            return null;
        }
        return user.getUserAccount();
    }
}
